package cn.wolfcode.util;

import cn.wolfcode.domain.Employee;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

public class UserContext {
    public static final String EMPLOYEE_IN_SESSIOM="EMPLOYEE_IN_SESSIOM";
    public static final String PERMISSION_IN_SESSIOM="PERMISSION_IN_SESSIOM";

    public static HttpSession getSession(){
//        SpringMVC 内置的工具类,可以获取到request,response,session
        ServletRequestAttributes attrs =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
               return attrs.getRequest().getSession();
    }

//    把员工和权限信息都设置和得到
//    员工得到
    public static Employee getUser(){
        return (Employee) getSession().getAttribute(UserContext.EMPLOYEE_IN_SESSIOM);
    }
//    员工设置
    public static void setUser(Employee employee){
        getSession().setAttribute(UserContext.EMPLOYEE_IN_SESSIOM,employee);
    }

//权限设置
    public static void setPermission(List<String> list){
        getSession().setAttribute(UserContext.PERMISSION_IN_SESSIOM,list);
    }

//    权限获取
    public static List<String> getPermission(){
       return (List<String>) getSession().getAttribute(UserContext.PERMISSION_IN_SESSIOM);
    }

}
